package com.ssy.lingxi.member.merchant.api.handler.validator;

import com.ssy.lingxi.member.merchant.api.handler.annotation.EnableDisableStatusAnno;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 状态枚举注解校验类
 * @author 万宁
 * @version 2.0.0
 * @date 2020-06-15
 */
public class EnableDisableStatusValidator implements ConstraintValidator<EnableDisableStatusAnno, Integer> {
    private boolean required = false;

    @Override
    public void initialize(EnableDisableStatusAnno constraintAnnotation) {
        required = constraintAnnotation.required();
    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        if(!required) {
            return true;
        }

        if(value == null) {
            return false;
        }

        return value >= 0 && value <= 1;
    }
}
